package astra.client.pages.clients;

import astra.client.Astra;
import astra.client.models.Client;
import astra.client.services.AstraServiceAsync;
import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.*;

/**
 * @author Kotelnikov Dmitry <a href="mailto:dimonnot@mail.ru"/>
 */
public class ClientPopup extends DialogBox{
    interface ClientPopupUiBinder extends UiBinder<HTMLPanel, ClientPopup> {
    }

    private static ClientPopupUiBinder ourUiBinder = GWT.create(ClientPopupUiBinder.class);

    @UiField
    TextBox name;
    @UiField
    TextBox company;
    @UiField
    TextBox address;
    @UiField
    TextBox phone;
    @UiField
    TextBox fax;
    @UiField
    TextBox email;
    @UiField
    TextBox contact;
    @UiField
    TextArea description;
    @UiField
    SimplePanel message;
    @UiField
    Button storeClient;
    @UiField
    Button closeButton;

    protected Client client;
    protected AstraServiceAsync service;
    protected Astra astra;

    public ClientPopup(Astra astra, AstraServiceAsync service) {
        HTMLPanel rootElement = ourUiBinder.createAndBindUi(this);

        setWidth("550px");
        this.astra = astra;
        this.service = service;
        setWidget(rootElement);
        setModal(true);
        setGlassEnabled(true);
        setAutoHideEnabled(true);
    }

    public void edit(Client client) {
        if (client != null) {
            setText("Редактирование данных о клиенте");
            this.client = client;
        } else {
            setText("Добавление клиента");
            this.client = new Client();
        }

        name.setValue(this.client.getName());
        company.setValue(this.client.getCompany());
        address.setValue(this.client.getAddress());
        email.setValue(this.client.getEmail());
        phone.setValue(this.client.getPhone());
        fax.setValue(this.client.getFax());
        contact.setValue(this.client.getContact());
        description.setValue(this.client.getDescription());

        center();
        show();
    }

    protected Client getClient() {
        client.setAddress(address.getValue());
        client.setName(name.getValue());
        client.setFax(fax.getValue());
        client.setPhone(phone.getValue());
        client.setCompany(company.getValue());
        client.setContact(contact.getValue());
        client.setDescription(description.getValue());
        return client;
    }

    @UiHandler("closeButton")
    public void onClickCloseButton(ClickEvent event) {
        hide();
    }

    @UiHandler("storeClient")
    public void onClickStoreClient(ClickEvent event) {
        if (name.getValue().isEmpty()) {
            astra.onFailure(null, "Не указано название клиента", message);
            return;
        }

        storeClient.setEnabled(false);

        service.store(getClient(), new AsyncCallback<Void>() {
            @Override
            public void onFailure(Throwable caught) {
                storeClient.setEnabled(true);
                astra.onFailure(caught, "Ошибка при сохранении информации о клиенте", message);
            }

            @Override
            public void onSuccess(Void result) {
                if (client.getId() < 1) {
                    astra.getAstraData().getClients().add(client);
                }
                storeClient.setEnabled(true);
                hide();
                astra.onSuccess("Информация сохранена", message);
            }
        });
    }
}